package br.com.timesystem.daoimpl;

import br.com.timesystem.dao.GenericDAO;
import java.io.Serializable;
import org.apache.commons.logging.*;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

/**
 *
 * @author duduso
 */
@Repository("genericDAO")
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class GenericDAOImpl<T> implements GenericDAO<T>{
    
    private static Log LOG = LogFactory.getLog(GenericDAOImpl.class);
    
    @Autowired
    private SessionFactory sessionFactory;
    
    @Override
    public Serializable salvar(T value){
        Serializable id = getSession().save(value);
        getSession().flush();
        return id;
    }
    
    @Override
    public void atualizar(T value){
        getSession().update(value);
        getSession().flush();
    }
    
    @Override
    public void salvarOuAtualizar(T value){
        getSession().saveOrUpdate(value);
        getSession().flush();
    }
    
    @Override
    public void excluir(T value){
        getSession().delete(value);
        getSession().flush();
    }
    
    @Override
    public T obterPorId(Class clazz, Serializable id){
        return (T) getSession().get(clazz, id);
    }
    
    public Session getSession(){
        return this.sessionFactory.getCurrentSession();
    }
    
}
